﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace Nirvana
{
    public static class AnimationCurveExtensions
    {

        public static string ToData(this AnimationCurve curve)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(curve.preWrapMode);
            stringBuilder.Append(',');
            stringBuilder.Append(curve.postWrapMode);
            stringBuilder.Append(',');
            stringBuilder.Append(curve.length);
            foreach (Keyframe keyframe in curve.keys)
            {
                stringBuilder.Append(',');
                stringBuilder.Append(keyframe.time);
                stringBuilder.Append(',');
                stringBuilder.Append(keyframe.value);
                stringBuilder.Append(',');
                stringBuilder.Append(keyframe.inTangent);
                stringBuilder.Append(',');
                stringBuilder.Append(keyframe.outTangent);
                stringBuilder.Append(',');
                stringBuilder.Append(keyframe.tangentMode);
            }
            return stringBuilder.ToString();
        }
        public static AnimationCurve TryParseFromData(string text)
        {
            string[] array = text.Split(new char[]
            {
                ','
            });
            if (array.Length < 3)
            {
                return null;
            }
            int preWrapMode;
            if (!int.TryParse(array[0], out preWrapMode))
            {
                return null;
            }
            int postWrapMode;
            if (!int.TryParse(array[1], out postWrapMode))
            {
                return null;
            }
            int num;
            if (!int.TryParse(array[2], out num))
            {
                return null;
            }
            if (array.Length != 5 * num + 3)
            {
                return null;
            }
            Keyframe[] array2 = new Keyframe[num];
            for (int i = 0; i < num; i++)
            {
                int num2 = 5 * i + 3;
                float num3;
                if (!float.TryParse(array[num2], out num3))
                {
                    return null;
                }
                float num4;
                if (!float.TryParse(array[num2 + 1], out num4))
                {
                    return null;
                }
                float num5;
                if (!float.TryParse(array[num2 + 2], out num5))
                {
                    return null;
                }
                float num6;
                if (!float.TryParse(array[num2 + 3], out num6))
                {
                    return null;
                }
                int tangentMode;
                if (!int.TryParse(array[num2 + 4], out tangentMode))
                {
                    return null;
                }
                Keyframe keyframe = new Keyframe(num3, num4, num5, num6);
                keyframe.tangentMode = tangentMode;
                array2[i] = keyframe;
            }
            return new AnimationCurve(array2)
            {
                preWrapMode = (WrapMode)preWrapMode,
                postWrapMode = (WrapMode)postWrapMode
            };
        }


    }
}

